<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="内容标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入内容标题"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="内容类型" prop="contentType">
        <el-select
          v-model="queryParams.contentType"
          placeholder="请选择内容类型"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.CONTENT_TYPE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable class="!w-240px">
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.COMMON_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['system:management:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['system:management:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
        <el-button
          type="danger"
          plain
          :disabled="isEmpty(checkedIds)"
          @click="handleDeleteBatch"
          v-hasPermi="['system:management:delete']"
        >
          <Icon icon="ep:delete" class="mr-5px" /> 批量删除
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table
      row-key="id"
      v-loading="loading"
      :data="list"
      :stripe="true"
      :show-overflow-tooltip="true"
      @selection-change="handleRowCheckboxChange"
    >
      <el-table-column type="selection" width="55" fixed="left" />
      <el-table-column label="内容编号" align="center" prop="id" width="100" fixed="left" />
      <el-table-column
        label="内容标题"
        align="center"
        prop="title"
        width="200"
        show-overflow-tooltip
      />
      <el-table-column label="内容类型" align="center" prop="contentType" width="120">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.CONTENT_TYPE" :value="scope.row.contentType" />
        </template>
      </el-table-column>
      <el-table-column label="内容正文" align="center" prop="content" width="120">
        <template #default="scope">
          <el-button link type="primary" @click="handleViewContent(scope.row.content)">
            <Icon icon="ep:view" class="mr-5px" /> 查看正文
          </el-button>
        </template>
      </el-table-column>
      <el-table-column
        label="内容摘要"
        align="center"
        prop="summary"
        width="200"
        show-overflow-tooltip
      />
      <el-table-column label="状态" align="center" prop="status" width="100">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.COMMON_STATUS" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="排序" align="center" prop="sort" width="80" />
      <el-table-column label="是否置顶" align="center" prop="isTop" width="100">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.INFRA_BOOLEAN_STRING" :value="scope.row.isTop" />
        </template>
      </el-table-column>
      <!-- <el-table-column label="浏览次数" align="center" prop="viewCount" width="100" />
      <el-table-column
        label="版本号"
        align="center"
        prop="version"
        width="120"
        show-overflow-tooltip
      />
      <el-table-column
        label="生效时间"
        align="center"
        prop="effectiveDate"
        :formatter="dateFormatter"
        width="180"
      />
      <el-table-column
        label="失效时间"
        align="center"
        prop="expiryDate"
        :formatter="dateFormatter"
        width="180"
      />
      <el-table-column label="标签" align="center" prop="tags" width="150" show-overflow-tooltip /> -->
      <!-- <el-table-column
        label="附件URL"
        align="center"
        prop="attachmentUrl"
        width="150"
        show-overflow-tooltip
      />
      <el-table-column
        label="SEO标题"
        align="center"
        prop="seoTitle"
        width="150"
        show-overflow-tooltip
      />
      <el-table-column
        label="SEO关键词"
        align="center"
        prop="seoKeywords"
        width="150"
        show-overflow-tooltip
      />
      <el-table-column
        label="SEO描述"
        align="center"
        prop="seoDescription"
        width="200"
        show-overflow-tooltip
      /> -->
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180"
      />
      <el-table-column label="操作" align="center" width="150" fixed="right">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['system:management:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['system:management:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <ManagementForm ref="formRef" @success="getList" />

  <!-- 内容正文查看弹窗 -->
  <Dialog v-model="contentDialogVisible" title="内容正文" width="900px">
    <div class="content-dialog">
      <el-scrollbar max-height="600px">
        <div class="content-view" v-html="currentContent"></div>
      </el-scrollbar>
    </div>
  </Dialog>
</template>

<script setup lang="ts">
import { isEmpty } from '@/utils/is'
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { ManagementApi, ManagementVO } from '@/api/system/systemConfig/management'
import ManagementForm from './ManagementForm.vue'
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'

/** 内容管理 列表 */
defineOptions({ name: 'Management' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<ManagementVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  title: undefined,
  contentType: undefined,
  status: undefined
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中
const contentDialogVisible = ref(false) // 内容正文查看弹窗
const currentContent = ref('') // 当前查看的内容

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await ManagementApi.getManagementPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await ManagementApi.deleteManagement(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 批量删除内容管理 */
const handleDeleteBatch = async () => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    await ManagementApi.deleteManagementList(checkedIds.value)
    checkedIds.value = []
    message.success(t('common.delSuccess'))
    await getList()
  } catch {}
}

const checkedIds = ref<number[]>([])
const handleRowCheckboxChange = (records: ManagementVO[]) => {
  checkedIds.value = records.map((item) => item.id)
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await ManagementApi.exportManagement(queryParams)
    download.excel(data, '内容管理.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 查看内容正文 */
const handleViewContent = (content: string) => {
  currentContent.value = content || '暂无内容'
  contentDialogVisible.value = true
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>

<style scoped lang="scss">
.content-dialog {
  background-color: var(--el-bg-color);
  border-radius: 4px;
}

.content-view {
  min-height: 100px;
  padding: 24px;
  line-height: 1.8;
  color: var(--el-text-color-primary);
  word-wrap: break-word;
  background-color: var(--el-bg-color);

  :deep(img) {
    height: auto;
    max-width: 100%;
  }

  :deep(p) {
    margin: 10px 0;
    color: var(--el-text-color-primary);
  }

  :deep(h1),
  :deep(h2),
  :deep(h3),
  :deep(h4),
  :deep(h5),
  :deep(h6) {
    margin: 15px 0 10px;
    font-weight: bold;
    color: var(--el-text-color-primary);
  }

  :deep(ul),
  :deep(ol) {
    padding-left: 20px;
    color: var(--el-text-color-primary);
  }

  :deep(li) {
    color: var(--el-text-color-primary);
  }

  :deep(table) {
    width: 100%;
    margin: 10px 0;
    background-color: var(--el-bg-color);
    border-collapse: collapse;
  }

  :deep(table td),
  :deep(table th) {
    padding: 8px;
    color: var(--el-text-color-primary);
    background-color: var(--el-bg-color);
    border: 1px solid var(--el-border-color);
  }

  :deep(table th) {
    font-weight: 600;
    background-color: var(--el-fill-color-light);
  }

  :deep(blockquote) {
    padding: 10px 15px;
    margin: 10px 0;
    color: var(--el-text-color-secondary);
    background-color: var(--el-fill-color-lighter);
    border-left: 4px solid var(--el-color-primary);
    border-radius: 4px;
  }

  :deep(code) {
    padding: 2px 6px;
    font-family: 'Courier New', Consolas, monospace;
    color: var(--el-color-danger);
    background-color: var(--el-fill-color-light);
    border: 1px solid var(--el-border-color-lighter);
    border-radius: 3px;
  }

  :deep(pre) {
    padding: 15px;
    overflow-x: auto;
    color: var(--el-text-color-primary);
    background-color: var(--el-fill-color);
    border: 1px solid var(--el-border-color);
    border-radius: 6px;

    code {
      padding: 0;
      color: inherit;
      background-color: transparent;
      border: none;
    }
  }

  :deep(a) {
    color: var(--el-color-primary);
    text-decoration: none;

    &:hover {
      color: var(--el-color-primary-light-3);
      text-decoration: underline;
    }
  }

  :deep(strong),
  :deep(b) {
    font-weight: 600;
    color: var(--el-text-color-primary);
  }
}
</style>
